<template>
  <div>
    <a-card :bordered="false" class="title-main">
      <a-row :gutter="[0,16]">
        <a-col :span="24">
          <h3 class="rel-row1-name"><a-icon type="bank" class="rel-row1-name-icon"/>{{ leaseData.roomName}}
          </h3>
        </a-col>
      </a-row>
      <a-divider />
      <a-row :gutter="[0,16]" class="title-it">
        <a-col :span="6">
          承租人：{{ leaseData.name}}
        </a-col>
        <a-col :span="6">
          聯係電話：{{ leaseData.phone}}
        </a-col>
        <a-col :span="12">
          <div>
          <a-button @click="$refs.leaseDetail.edit(leaseData.id)" type="primary" ghost class="hhBum">
            租約詳情
          </a-button>
          <a-button @click="$refs.houseBillYjForm.edit(leaseId)" v-if="leaseData.leaseStatus === 1" type="primary" ghost class="hhBum">
              押金调整
          </a-button>
          <a-button @click="$refs.houseBillPlanForm.edit(leaseData)" v-if="leaseData.leaseStatus === 1" type="primary" ghost class="hhBum">
            租金調整
          </a-button>
         <a-button @click="$refs.xuzuForm.edit(leaseId)" v-if="leaseData.leaseStatus !== 2 " type="primary" ghost class="hhBum">
            續租
          </a-button>
          <a-button v-if="leaseData.leaseStatus === 1" @click="$refs.tuiForm.edit(leaseId)" type="primary" ghost class="hhBum">
            退租
          </a-button>
        </div>
        </a-col>
      </a-row>
      <a-row :gutter="[0,16]" class="title-it">
        <a-col :span="6">
          租金：{{ leaseData.rent}}
        </a-col>
        <a-col :span="6">
          租期：{{ leaseData.rentStartDate}}~{{ leaseData.rentEndDate}}
        </a-col>
      </a-row>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="middle"
        :columns="columns"
        :data="loadData"
        :extraTool="[]"
        bordered
        :rowKey="(record) => record.id"
      >
        <template slot="rentQs" slot-scope="text,record">
          <span v-if="(record.rentedDay <moment().format('YYYY-MM-DD'))&&(record.billStatus  === 0)">
             <a-tag color="red">
              逾期
            </a-tag>
          </span>
          {{ text }}
        </template>
        <template slot="rentStartDate" slot-scope="text,record">
          {{ text }}~{{ record.rentEndDate }}
        </template>
        <template slot="rentAmountName" slot-scope="text,record">
          <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
            <a-col :span="24" class="aa"><span>{{ item.rentAmountName }}</span></a-col>
          </a-row>
        </template>
        <template slot="amount" slot-scope="text,record">
          <a-row v-for="(item,index) in record.details" :key="index" class="aaa">
            <a-col :span="24" :class="item.amount < 0 ? 'abvv': 'ab'" class="aa"><span>{{ item.amount }}</span></a-col>
          </a-row>
        </template>
        <template slot="subtotal" slot-scope="text">
         <span  :class="text < 0 ? 'abvv': 'ab' ">{{ text }}</span>
        </template>
        <template slot="receiveAmount" slot-scope="text">
          <span class="ab">{{ text }}</span>
        </template>
        <template slot="collectionType" slot-scope="text">
          <span>{{ statusFkfsFilter(text) }}</span>
        </template>
        <template slot="billStatus" slot-scope="text">
          <span v-if="text === 0">
             <a-tag color="orange" style="font-weight:bold;font-size:15px">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span v-else-if="text === 1">
             <a-tag color="green" style="font-weight:bold;font-size:15px">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span  v-else-if="text === 3">
             <a-tag color="purple" style="font-weight:bold;font-size:15px">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
          <span  v-else>
             <a-tag color="cyan" style="font-weight:bold;font-size:15px">
              {{ statusFilter(text) }}
            </a-tag>
          </span>
        </template>
        <span slot="operation" slot-scope="text, record">
          <a @click="$refs.readDetailForm.edit(record,leaseData.roomName,leaseData.name,2)">查看</a>
          <a-divider type="vertical" />
            <a-dropdown >
              <a class="ant-dropdown-link">
                更多 <a-icon type="down" />
              </a>
              <a-menu slot="overlay">
                <a-menu-item v-if="record.billStatus ===0">
                  <a @click="$refs.arriveForm.edit(record)">到賬</a>
                </a-menu-item>
                <a-menu-item v-if="record.hyCb ===1 && (record.billStatus ===0 || record.billStatus ===3)">
                  <a @click="$refs.addFormMeterOne.add(record)">抄表</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===0 || record.billStatus ===3">
                  <a @click="$refs.editFormOne.edit(record,leaseData.roomName,leaseData.name,1)">修改</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===0">
                  <a @click="$refs.tuoFormOne.edit(record)">拖欠</a>
                </a-menu-item>
                <a-menu-item v-if="record.billStatus ===1">
                  <a-popconfirm  placement="topRight" title="撤銷到賬將刪除此賬單到賬時產生的流水？" @confirm="() => chexiaoDaoZhang(record.id)">
                    <a>撤銷到賬</a>
                  </a-popconfirm>
                </a-menu-item>
                 <a-menu-item v-if="record.billStatus ===3">
                  <a @click="$refs.tzqsForm.edit(record.id)">退租清算</a>
                </a-menu-item>
                <a-menu-item>
                  <a @click="$refs.printBillDetail.edit(record.id)">打印</a>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
        </span>
      </s-table>
    </a-card>
    <add-form-meter-one ref="addFormMeterOne" @ok="handleOk" />
    <edit-form-one ref="editFormOne" @ok="handleOk" />
    <tuo-form-one ref="tuoFormOne" @ok="handleOk" />
    <arrive-form ref="arriveForm" @ok="handleOk" />
    <tui-form ref="tuiForm" @ok="handleOk" />
    <tzqs-form ref="tzqsForm" @ok="handleOk" />
    <xuzu-form ref="xuzuForm" @ok="handleOk" />
    <read-detail-form ref="readDetailForm"/>
    <print-bill-detail ref="printBillDetail"/>
    <house-bill-plan-form ref="houseBillPlanForm" @ok="handleOk" />
    <house-bill-yj-form ref="houseBillYjForm" @ok="handleOk" />
    <lease-detail ref="leaseDetail" @ok="handleOk" />
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { houseBillPageByLeaseId, houseBillcheXiaoDaoZhangEdit } from '@/api/modular/main/housebill/houseBillManage'
  import { houseLeasedetail } from '@/api/modular/main/houselease/houseLeaseManage'
  import { busMeansOfTransactionList } from '@/api/modular/main/busmeansoftransaction/busMeansOfTransactionManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import addFormMeterOne from '../hydropowermeter/addFormMeterOne.vue'
  import editFormOne from './editFormOne.vue'
  import tuoFormOne from './tuoFormOne.vue'
  import arriveForm from './arriveForm.vue'
  import tuiForm from './tuiForm.vue'
  import tzqsForm from './tzqsForm.vue'
  import xuzuForm from './xuzuForm.vue'
  import readDetailForm from './readDetailForm.vue'
  import printBillDetail from './printBillDetail.vue'
  import houseBillPlanForm from './houseBillPlanForm.vue'
  import houseBillYjForm from './houseBillYjForm.vue'
  import leaseDetail from '../houselease/leaseDetail.vue'
  import moment from 'moment'
  export default {
    components: {
      STable,
      addFormMeterOne,
      editFormOne,
      tuoFormOne,
      arriveForm,
      tuiForm,
      tzqsForm,
      xuzuForm,
      readDetailForm,
      printBillDetail,
      houseBillPlanForm,
      leaseDetail,
      houseBillYjForm
    },
    data () {
      return {
          // 查询参数
        queryParam: {
          pageSize: 10
        },
        leaseId: '',
        // 表头
        columns: [
          {
            title: '期數',
            align: 'center',
            dataIndex: 'qs',
            scopedSlots: { customRender: 'rentQs' }
          },
          {
            title: '收租日',
            align: 'center',
            dataIndex: 'rentedDay'
          },
          {
            title: '賬單周期',
            align: 'center',
            dataIndex: 'rentStartDate',
            scopedSlots: { customRender: 'rentStartDate' }
          },
          {
            title: '類別',
            align: 'center',
            dataIndex: 'rentAmountName',
            scopedSlots: { customRender: 'rentAmountName' }
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'amount',
            scopedSlots: { customRender: 'amount' }
          },
          {
            title: '小計',
            align: 'center',
            dataIndex: 'subtotal',
            scopedSlots: { customRender: 'subtotal' }
          },
          {
            title: '實收金額',
            align: 'center',
            dataIndex: 'receiveAmount',
            scopedSlots: { customRender: 'receiveAmount' }
          },
          {
            title: '收款日期',
            align: 'center',
            dataIndex: 'receiveDate'
          },
          {
            title: '收款方式',
            align: 'center',
            dataIndex: 'collectionType',
            scopedSlots: { customRender: 'collectionType' }
          },
          {
            title: '到账状态',
            align: 'center',
            dataIndex: 'billStatus',
            scopedSlots: { customRender: 'billStatus' }
          },
          {
            title: '操作',
            key: 'action',
            scopedSlots: { customRender: 'operation' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // loadData: [],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return houseBillPageByLeaseId(Object.assign(parameter, this.queryParam, { houseLeaseId: this.leaseId })).then((res) => {
            return res.data
          })
        },
        billStatusDictTypeDropDown: [],
        leaseData: {},
        collectionTypeData: []
      }
    },
    created () {
     this.leaseId = this.$route.query.id
     this.initNeedData()
     this.sysDictTypeDropDown()
     this.getLeaseDetail(this.leaseId)
    },
    methods: {
      moment,
      initNeedData() {
        busMeansOfTransactionList().then((res) => {
          if (res.code === 200) {
            this.collectionTypeData = res.data
          } else {
            this.$message.error(res.msg)
          }
        })
      },
      getLeaseDetail(id) {
        houseLeasedetail({ id: id }).then((res) => {
          if (res.code === 200) {
            this.leaseData = res.data
          } else {
          }
        })
      },
      /**
       * 获取字典数据
       */
      sysDictTypeDropDown () {
        sysDictTypeDropDown({ code: 'bill_status' }).then((res) => {
           this.billStatusDictTypeDropDown = res.data
        })
      },
      statusFkfsFilter (id) {
        // eslint-disable-next-line eqeqeq
        const values = this.collectionTypeData.filter(item => item.id == id)
        if (values.length > 0) {
          return values[0].name
        }
      },
      statusFilter (sex) {
        // eslint-disable-next-line eqeqeq
        const values = this.billStatusDictTypeDropDown.filter(item => item.code == sex)
        if (values.length > 0) {
          return values[0].value
        }
      },
      handleOk() {
        this.getLeaseDetail(this.leaseId)
        this.$refs.table.refresh()
      },
      chexiaoDaoZhang(id) {
        houseBillcheXiaoDaoZhangEdit({ id: id }).then((res) => {
          if (res.code === 200) {
            this.handleOk()
          } else {
            this.$message.error(res.msg)//  + res.message
          }
        })
      }
    }
  }
</script>
<style lang="less">
  .title-it{
    background-color: rgb(243, 241, 241);
    padding-left: 10px;
    font-size: 16px;
  }
  .s-table-tool{
    display:none;
  }
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .aaa{
    border-bottom: 1px solid #ccc;
    margin-bottom: 6px;
  }
  .aa{
    text-align: center;
    font-size: 16px;
  }
  .ab{
    color: red;
  }
  .abvv{
    color: green;
  }
  .title-main{
    .ant-card-body{
      padding: 0px 24px;
    }
  }
  .hhBum{
    float: right;
  }
</style>
